package com.zhongshengnetwork.rightbe.my.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.dbmodel.ShopCardbModel;
import com.zhongshengnetwork.rightbe.dbservice.LogindbService;
import com.zhongshengnetwork.rightbe.dbservice.ShopCardbService;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;

import java.util.HashMap;
import java.util.Map;
//三检查OK:2016-11-30
public class EditActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{
    private TopBarView topbar;
    private EditText editText;
    public static final int NICKNAME_REQUSET = 1004;//昵称
    public static final int SIGN_REQUEST = 1005;//签名
    private int type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getIntent().getIntExtra("type",0);
        String name = getIntent().getStringExtra("name");
        setContentView(R.layout.activity_edit);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        topbar.getRightTextView().setText("保存");
        topbar.getRightTextView().setTextSize(18);
        topbar.getRightTextView().setTextColor(getResources().getColor(R.color.defaultLightColor));
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hintKeyBoard();
                if (editText.getText().toString().trim().length() > 0){
                   changeUserInfo();
                }
            }
        });
        if (type == NICKNAME_REQUSET){
            topbar.getTitleView().setText("设置昵称");
            if (CommonUtils.isEmpty(name)){
                name = "请输入昵称";
            }
        }else if (type == SIGN_REQUEST){
            topbar.getTitleView().setText("个性签名");
            if (CommonUtils.isEmpty(name)){
                name = "请输入个性签名";
            }
        }
        editText = (EditText) findViewById(R.id.edit_text);
        editText.setHint(name);
        editText.addTextChangedListener(new Watcher(editText));

        LinearLayout edit_layout = (LinearLayout) findViewById(R.id.edit_layout);
        edit_layout.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());
        RelativeLayout edit_main = (RelativeLayout) findViewById(R.id.edit_main);
        edit_main.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        editText.setHintTextColor(AppThemeUtils.getInstance().getLightTextColor());
        editText.setTextColor(AppThemeUtils.getInstance().getUserNameColorOfWeiJu());
    }

    public void hintKeyBoard() {
        //拿到InputMethodManager
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        //如果window上view获取焦点 && view不为空
        if (imm.isActive() && getCurrentFocus() != null) {
            //拿到view的token 不为空
            if (getCurrentFocus().getWindowToken() != null) {
                //表示软键盘窗口总是隐藏，除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    //修改个人信息
    private void changeUserInfo(){
        final Map<String,String> map = new HashMap<String, String>();
        map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        if (type == NICKNAME_REQUSET){
            map.put(APIKey.nicknameKey,editText.getText().toString());
        }else if (type == SIGN_REQUEST){
            map.put(APIKey.signatureKey,editText.getText().toString());
        }
        Log.e("TAG","请求参数："+map.toString()+"\n");
        HttpsUtils.changeuserinfoDo(map, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                Log.e("TAG","请求结果："+data+"\n");
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    if (type == NICKNAME_REQUSET){
                        CustomApplication.loginModel.setNickname(editText.getText().toString());
                    }else if (type == SIGN_REQUEST){
                        CustomApplication.loginModel.setSignature(editText.getText().toString());
                    }
                    LogindbService.saveOrUpdateLogindbMode(CustomApplication.loginModel);
                    setResult(RESULT_OK);
                    finish();
                }else {
                    CustomApplication.showTip(commonModel,EditActivity.this);
                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        hintKeyBoard();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    public class Watcher implements TextWatcher
    {
        private EditText EditID = null;
        public Watcher(EditText id){
            EditID = id;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if (editText.getText().toString().trim().length() > 0){
                topbar.getRightTextView().setTextColor(getResources().getColor(R.color.mainColor));
            }else {
                topbar.getRightTextView().setTextColor(getResources().getColor(R.color.defaultLightColor));
            }
        }

    }
}
